/**
 * 
 */
package com.alstudio.ui.module.user;

import android.content.Context;
import android.os.Bundle;
import android.text.InputFilter;
import android.text.TextUtils;
import android.view.View;

import com.alstudio.app.ALLocalEnv;
import com.alstudio.module.xmpp.request.ALUiXmppRequestCreator;
import com.alstudio.pojo.module.user.User;
import com.alstudio.ui.base.TitleBarActivity;
import com.alstudio.utils.formater.string.StringUtils;
import com.alstudio.utils.log.ALLog;
import com.alstudio.view.edittext.ALEditText;
import com.alstudio.view.edittext.ALEditText.OnALEditorActionListener;
import com.alstudio.view.toast.MyToastUtil;
import com.loovee.imaohu.R;

/**
 * 实现输入功能,目前支持修改昵称、签名
 * 
 * @author alonso lee
 * 
 */
public class InputActivity extends TitleBarActivity implements
		OnALEditorActionListener {

	// 修改昵称
	public static final int INPUT_TYPE_NICK = 0;
	// 修改签名
	public static final int INPUT_TYPE_SIG = 1;
	// 设置备注名
	public static final int INPUT_TYPE_NOTE_NAME = 2;
	// 修改方式，缺省为修改昵称
	private int inputType = INPUT_TYPE_NICK;

	// 输入框
	private ALEditText inputEditText;

	// 昵称只支持13个字符
	private final int NICK_LENTH = 13;
	// 签名支持50个字符
	private final int SIG_LENTH = 50;

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.alstudio.ui.base.TitleBarActivity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setupView(this, getClass().getSimpleName());
		initData();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.alstudio.ui.base.BaseActivity#setupView(android.content.Context,
	 * java.lang.String)
	 */
	@Override
	protected void setupView(Context context, String name) {
		// TODO Auto-generated method stub
		super.setupView(context, name);
		setTitleBarContentView(R.layout.input_layout);
//		setRightButtonBackground(R.drawable.friend_title_icon_confirm);
		rightUseImageButton(true);
		setRightButtonImageSrc(R.drawable.friend_title_icon_confirm);

		inputEditText = (ALEditText) findViewById(R.id.input);
		inputEditText.setALEditorActionListener(this);
		setRightButtonClickListener(this);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.alstudio.ui.base.BaseActivity#initData()
	 */
	@Override
	protected void initData() {
		// TODO Auto-generated method stub
		inputType = getIntent().getIntExtra("type", INPUT_TYPE_NICK);
		initUI();
	}

	private void initUI() {
		// 签名只能输入13个字符
		int maxLenth = NICK_LENTH;
		String str = "";
		switch (inputType) {
		case INPUT_TYPE_NICK:
			setCenterTextViewMessage(R.string.TxtNick);
			str = ALLocalEnv.getLocalEnv().getMyProfile().getNick();
			inputEditText.setMaxLines(1);
			break;
		case INPUT_TYPE_SIG:
			setCenterTextViewMessage(R.string.TxtModifySig);
			maxLenth = SIG_LENTH;
			str = ALLocalEnv.getLocalEnv().getMyProfile().getSignature();
			break;
		case INPUT_TYPE_NOTE_NAME:
			setCenterTextViewMessage(R.string.TxtNoteName);
			break;
		}

		InputFilter[] FilterArray = new InputFilter[1];
		FilterArray[0] = new InputFilter.LengthFilter(maxLenth);
		inputEditText.setFilters(FilterArray);

		inputEditText.setText(str);
		inputEditText.setSelection(str.length());
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.alstudio.view.edittext.ALEditText.OnALEditorActionListener#onEditorAction
	 * ()
	 */
	@Override
	public void onEditorAction() {
		// TODO Auto-generated method stub

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.alstudio.view.edittext.ALEditText.OnALEditorActionListener#onContentClear
	 * ()
	 */
	@Override
	public void onContentClear() {
		// TODO Auto-generated method stub

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.alstudio.view.edittext.ALEditText.OnALEditorActionListener#
	 * onContentChange(java.lang.String)
	 */
	@Override
	public void onContentChange(String content) {
		// TODO Auto-generated method stub
		boolean isFilled = content.length() != 0;
		getRightButton().setEnabled(isFilled);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.alstudio.ui.base.TitleBarActivity#onClick(android.view.View)
	 */
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		super.onClick(v);
	}
	
	/* (non-Javadoc)
	 * @see com.alstudio.ui.base.TitleBarActivity#invokeRightBtnClick()
	 */
	@Override
	protected void invokeRightBtnClick() {
		// TODO Auto-generated method stub
		super.invokeRightBtnClick();
		invokeCommitClick();
	}

	private void invokeCommitClick() {
		if (!ALLocalEnv.isSendXmppDataEnable())
			return;
		String str = inputEditText.getText().toString();
		User u = new User();
		switch (inputType) {
		case INPUT_TYPE_NICK:
			String s = StringUtils
					.getNickBlockStr(getApplicationContext(), str);
			if (!TextUtils.isEmpty(s)) {
				// 含有违禁字符
				String msg = String.format(
						getString(R.string.TxtBlockingKeywordAlert), s);
				MyToastUtil.getInstance().showToastOnCenter(msg);
				return;
			}
			ALLocalEnv.getLocalEnv().getMyProfile().setNick(str);
			u.setNick(str);
			break;
		case INPUT_TYPE_SIG:
			ALLocalEnv.getLocalEnv().getMyProfile().setSignature(str);
			u.setSignature(str);
			break;
		}
		ALUiXmppRequestCreator.postALUiXmppModifyUserProfileEvent(u);
		finish();
	}

}
